using System.Text;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using UniversalAdminSystem.Application.FileStorage.Interfaces;

namespace UniversalAdminSystem.Infrastructure.FileStorage.Parsers;

public class PdfParseService : IDocParser
{
    public async Task<string> ParseAsync(string path)
    {
        return await Task.Run(() =>
        {
            var text = new StringBuilder();
            using (var pdfReader = new PdfReader(path))
            {
                using var pdfDocument = new PdfDocument(pdfReader);
                int numberOfPages = pdfDocument.GetNumberOfPages();
                for (int i = 1; i <= numberOfPages; i++)
                {
                    var page = pdfDocument.GetPage(i);
                    var pageText = PdfTextExtractor.GetTextFromPage(page);
                    text.Append(pageText);
                }

            }
            return text.ToString();
        });
    }
}
